home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Over 1,000 Windows 95 Programs
/
Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso
/
0957
/
gnugrep
/
gui
/
dirmat~1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-18
|
11KB
|
311 lines
/***********************************************************************
* class CDirMatcher - Assists in processing directories to find
* files matching wildcard, attribute, date/time and filesize
* requirements.
*
* Copyright (C) D. Munro 1996
*
* This program source is freely redistributable.
************************************************************************
#include "stdafx.h"
#include "DirMatcher.h"
//#define _DEBUG_PRINT
void CDirMatcher::_SetDates_(COleDateTime *pStartTime, COleDateTime *pEndTime)
//----------------------------------------------------------------------------
{ if (pStartTime != NULL)
m_oledteStartTime = *pStartTime;
else
m_oledteStartTime = time_t(0);
if (pEndTime != NULL)
m_oledteEndTime = *pEndTime;
else
m_oledteEndTime = COleDateTime(9999,12,31,0,0,0);
}
void CDirMatcher::_SetSizes_(DWORD dwMinSizeLo, DWORD dwMinSizeHi,
DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
//----------------------------------------------------------------
{ m_qwMinSize = (__int64)dwMinSizeLo | ((__int64)dwMinSizeHi << 32);
m_qwMaxSize = (__int64)dwMaxSizeLo | ((__int64)dwMaxSizeHi << 32);
}
void CDirMatcher::_Constructor_(BOOL bMatchHidden, BOOL bMatchSystem,
BOOL bMatchReadOnly, BOOL bMatchDirectory,
BOOL bMatchNormal, BOOL bMatchArchive,
COleDateTime *pStartTime, COleDateTime *pEndTime,
DWORD dwMinSizeLo, DWORD dwMinSizeHi,
DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
//------------------------------------------------------------------------------
{ _SetDates_(pStartTime, pEndTime);
_SetSizes_(dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
m_bMatchReadOnly = bMatchReadOnly;
m_bMatchDirectory = bMatchDirectory;
m_bMatchHidden = bMatchHidden;
m_bMatchSystem = bMatchSystem;
m_bMatchNormal = bMatchNormal;
m_bMatchArchive = bMatchArchive;
}
CDirMatcher::CDirMatcher(CStringArray &strarrSpecs,
BOOL bMatchHidden, BOOL bMatchSystem,
BOOL bMatchReadOnly, BOOL bMatchDirectory,
BOOL bMatchNormal, BOOL bMatchArchive,
COleDateTime *pStartTime, COleDateTime *pEndTime,
DWORD dwMinSizeLo, DWORD dwMinSizeHi,
DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
//-----------------------------------------------------------------------
{ m_strarrFileSpecs.SetSize(strarrSpecs.GetSize(),10);
for (int i=0; i<strarrSpecs.GetSize(); i++)
m_strarrFileSpecs[i] = strarrSpecs[i];
_Constructor_(bMatchHidden, bMatchSystem, bMatchReadOnly, bMatchDirectory,
bMatchNormal, bMatchArchive, pStartTime, pEndTime,
dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
}
// Takes a ; delimited string of specs eg *.c;*.cpp;*.h
CDirMatcher::CDirMatcher(CString strSpecs,
BOOL bMatchHidden, BOOL bMatchSystem,
BOOL bMatchReadOnly, BOOL bMatchDirectory,
BOOL bMatchNormal, BOOL bMatchArchive,
COleDateTime *pStartTime, COleDateTime *pEndTime,
DWORD dwMinSizeLo, DWORD dwMinSizeHi,
DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
//-----------------------------------------------------------------------
{ _ParseSpecs_(strSpecs);
_Constructor_(bMatchHidden, bMatchSystem, bMatchReadOnly, bMatchDirectory,
bMatchNormal, bMatchArchive, pStartTime, pEndTime,
dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
}
void CDirMatcher::SetAttributes(BOOL bMatchReadOnly, BOOL bMatchDirectory,
BOOL bMatchHidden, BOOL bMatchSystem,
BOOL bMatchNormal, BOOL bMatchArchive)
//------------------------------------------------------------------------
{ m_bMatchReadOnly = bMatchReadOnly;
m_bMatchDirectory = bMatchDirectory;
m_bMatchHidden = bMatchHidden;
m_bMatchSystem = bMatchSystem;
m_bMatchNormal = bMatchNormal;
m_bMatchArchive = bMatchArchive;
}
void CDirMatcher::SetFileSpecs(CStringArray strarrSpecs)
//-------------------------------------------------------
{ m_strarrFileSpecs.RemoveAll();
m_strarrFileSpecs.SetSize(strarrSpecs.GetSize(),10);
for (int i=0; i<strarrSpecs.GetSize(); i++)
m_strarrFileSpecs[i] = strarrSpecs[i];
}
void CDirMatcher::AddFileSpec(CString strSpec)
//----------------------------------------
{ int nIndex = m_strarrFileSpecs.GetSize();
m_strarrFileSpecs.SetAtGrow(nIndex,strSpec);
}
int CDirMatcher::_ParseSpecs_(CString strSpecs)
//---------------------------------------------
{ m_strarrFileSpecs.RemoveAll();
m_strarrFileSpecs.SetSize(0,10);
CString strSpec;
int nSpecCnt =0;
strSpecs.TrimLeft(); strSpecs.TrimRight();
while (! strSpecs.IsEmpty())
{ int nSep = strSpecs.Find(';');
if (nSep == -1)
nSep = strSpecs.GetLength();
strSpec = strSpecs.Left(nSep);
strSpecs = strSpecs.Mid(nSep+1);
strSpec.TrimLeft(); strSpec.TrimRight();
strSpecs.TrimLeft(); strSpecs.TrimRight();
if (! strSpec.IsEmpty())
m_strarrFileSpecs.SetAtGrow(nSpecCnt++, strSpec);
}
return nSpecCnt;
}
void CDirMatcher::_SetSpecs_(CString strSpec, CString &strName, CString &strExt)
//------------------------------------------------------------------------------
{ strSpec.TrimLeft(); strSpec.TrimRight();
ASSERT(! strSpec.IsEmpty());
int nDot = strSpec.ReverseFind('.');
if (nDot > -1)
{ strExt = strSpec.Mid(nDot+1);
strName = strSpec.Left(nDot);
}
else
{ strExt = "*"; // Match a* means same as Match a*.*
strName = strSpec;
}
strName.TrimLeft(); strName.TrimRight();
strExt.TrimLeft(); strExt.TrimRight();
}
void CDirMatcher::_ExpandSpec_(CString &strExpNameSpec, int nNameLen)
//-------------------------------------------------------------------
{ BOOL bWildCard =FALSE;
int i;
for (i=strExpNameSpec.GetLength(); i<nNameLen; i++)
strExpNameSpec += (TCHAR)127;
#ifdef _DEBUG_PRINT
TRACE1("Exp Len = %d\n",strExpNameSpec.GetLength());
#endif
//ASSERT(strExpNameSpec.GetLength() == nNameLen);
for (i=0; i<nNameLen; i++)
{ TCHAR ch = strExpNameSpec[i];
if (ch == '*')
bWildCard = TRUE;
else
if (ch != '*' && ch != '?' && ch != (TCHAR)127)
bWildCard = FALSE;
else;
if (bWildCard)
strExpNameSpec.SetAt(i,'?');
else
strExpNameSpec.SetAt(i, ch);
}
for (i=0; i<strExpNameSpec.GetLength(); i++)
if (strExpNameSpec[i] == (TCHAR)127)
strExpNameSpec.SetAt(i,' ');
}
BOOL CDirMatcher::_MatchSpecToName_(CString strSpec, CString strName)
//-------------------------------------------------------------------
{ for (int i=0; i<strSpec.GetLength(); i++)
{ if (strSpec[i] == '?') continue;
if (i >= strName.GetLength()) return FALSE;
if (strSpec[i] == strName[i]) continue;
return FALSE;
}
return TRUE;
}
BOOL CDirMatcher::Match(WIN32_FIND_DATA *pFindData)
//-------------------------------------------------
{ CString strFileName(pFindData->cFileName);
strFileName.MakeUpper();
CString strName, strExt;
COleDateTime oledteFileTime = COleDateTime(pFindData->ftLastWriteTime);
__int64 qwSize = (__int64)pFindData->nFileSizeLow | ((__int64)pFindData->nFileSizeHigh << 32);
if (! m_bMatchHidden)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN)
{
#ifdef _DEBUG_PRINT
TRACE1("Hidden File %s rejected\n",strFileName);
#endif
return FALSE;
}
if (! m_bMatchSystem)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) == FILE_ATTRIBUTE_SYSTEM)
{
#ifdef _DEBUG_PRINT
TRACE1("System File %s rejected\n",strFileName);
#endif
return FALSE;
}
if (! m_bMatchReadOnly)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)
{
#ifdef _DEBUG_PRINT
TRACE1("Read Only File %s rejected\n",strFileName);
#endif
return FALSE;
}
if (! m_bMatchDirectory)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
#ifdef _DEBUG_PRINT
TRACE1("Directory %s rejected\n",strFileName);
#endif
return FALSE;
}
if (! m_bMatchNormal)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_NORMAL) == FILE_ATTRIBUTE_NORMAL)
{
#ifdef _DEBUG_PRINT
TRACE1("Normal File %s rejected\n",strFileName);
#endif
return FALSE;
}
if (! m_bMatchArchive)
if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE)
{
#ifdef _DEBUG_PRINT
TRACE1("Normal File %s rejected\n",strFileName);
#endif
return FALSE;
}
if ( (oledteFileTime.GetStatus() == COleDateTime::valid) && (m_oledteStartTime.GetStatus() == COleDateTime::valid))
if ( (oledteFileTime >= m_oledteStartTime) && (oledteFileTime <= m_oledteEndTime) )
{
#ifdef _DEBUG_PRINT
TRACE("Date/Time matches : %s in %s - %s\n",oledteFileTime.Format(),
m_oledteStartTime.Format(), m_oledteEndTime.Format());
#endif
}
else
{
#ifdef _DEBUG_PRINT
TRACE("Date/Time Fails : %s not in %s - %s\n",oledteFileTime.Format(),
m_oledteStartTime.Format(), m_oledteEndTime.Format());
#endif
return FALSE;
}
if ( (qwSize >= m_qwMinSize) && (qwSize <= m_qwMaxSize) )
{
#ifdef _DEBUG_PRINT
TRACE0("Size matches\n");
#endif
}
else
{
#ifdef _DEBUG_PRINT
TRACE("Date/Time Fails : %dl64 not in %dl64 - %dl64\n",qwSize, m_qwMinSize,
m_qwMaxSize);
#endif
return FALSE;
}
for (int i=0; i<m_strarrFileSpecs.GetSize(); i++)
{ CString strNameSpec, strExtSpec;
_SetSpecs_(m_strarrFileSpecs[i], strNameSpec, strExtSpec);
strNameSpec.MakeUpper(); strExtSpec.MakeUpper();
_SetSpecs_(strFileName, strName, strExt);
CString strExpNameSpec(strNameSpec), strExpExtSpec(strExtSpec);
_ExpandSpec_(strExpNameSpec, strName.GetLength());
_ExpandSpec_(strExpExtSpec, strExt.GetLength());
#ifdef _DEBUG_PRINT
TRACE2("Exp Name = %s, Exp Ext. = %s\n",strExpNameSpec,strExpExtSpec);
#endif
BOOL bMatch = _MatchSpecToName_(strExpNameSpec, strName);
if (! bMatch)
{
#ifdef _DEBUG_PRINT
TRACE2("Name match failed %s != %s \n",strExpNameSpec, strName);
#endif
continue;
}
#ifdef _DEBUG_PRINT
TRACE2("Name matches %s == %s \n",strExpNameSpec, strName);
#endif
bMatch = _MatchSpecToName_(strExpExtSpec, strExt);
if (! bMatch)
{
#ifdef _DEBUG_PRINT
TRACE2("Extension match fails %s != %s \n",strExpExtSpec, strExt);
#endif
continue;
}
#ifdef _DEBUG_PRINT
TRACE2("Extension matches %s == %s \n",strExpExtSpec, strExt);
#endif
return TRUE;
}
return FALSE;
}